import fs from "node:fs";
import path from "node:path";
import chalk from "chalk";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const failHandler = (type: string, msg: string, err: Error) => {
  console.log(chalk.red(type));
  console.log(chalk.red(msg));
  console.log(chalk.red(err?.message));
  process.exit(1);
};

export const getPkgInfo = () => {
  const pkgStr = fs.readFileSync(
    path.join(__dirname, "../package.json"),
    "utf-8",
  );
  return JSON.parse(pkgStr) as { name: string; version: string };
};
